﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TjZheng.Base;
using TjZheng.Framework;
using TjZheng.Power.Business;
using TjZheng.Power.DataEntity;

namespace TjZheng.CMS.Process.Business
{
    public static class GroupPowerInfoExtension
    {
        /// <summary>
        /// 树
        /// </summary>
        /// <param name="objPowerInfoList">权限列表</param>
        /// <param name="rootId">根节点Id</param>
        /// <param name="objCheckedPowerInfoList">选中权限</param>
        /// <returns></returns>
        public static TreeEntity ConvertToCheckedTree(this List<PowerInfo> objPowerInfoList, long rootId, List<PowerInfo> objCheckedPowerInfoList)
        {
            TreeEntity objTreeEntity = null;
            if (objPowerInfoList.IsNull())
            {
                return objTreeEntity;
            }
            var objRoot = objPowerInfoList.FirstOrDefault(c => c.Id == rootId);
            if (objRoot == null)
            {
                return objTreeEntity;
            }
            objTreeEntity = new TreeEntity();
            objTreeEntity.id = objRoot.Id;
            objTreeEntity.text = objRoot.PowerName;
            objTreeEntity.iconCls = objRoot.Icon;
            objTreeEntity.Data = objRoot;
            int count1 = objPowerInfoList.Count(c => c.IDPath.Contains(objRoot.IDPath));
            int count2 = objCheckedPowerInfoList.Count(c => c.IDPath.Contains(objRoot.IDPath));
            objTreeEntity.@checked = count1 == count2;
            if (objRoot.HasChild == 1)
            {
                var objChildren = objPowerInfoList.Where(c => c.ParentId == objRoot.Id).ToList();
                if (objChildren.IsNoNull())
                {
                    objTreeEntity.state = "closed";
                    TreeEntity objChildTreeEntity = null;
                    foreach (var objChild in objChildren.OrderBy(c => c.SortIndex))
                    {
                        objChildTreeEntity = ConvertToCheckedTree(objPowerInfoList, objChild.Id, objCheckedPowerInfoList);
                        if (objChildTreeEntity != null)
                        {
                            objTreeEntity.children.Add(objChildTreeEntity);
                        }
                    }
                }
            }
            return objTreeEntity;
        }
        /// <summary>
        /// 列表树（或树）
        /// </summary>
        /// <param name="objPowerInfoList">权限列表</param>
        /// <param name="rootId">根节点Id</param>
        /// <param name="objCheckedPowerInfoList">选中权限</param>
        /// <returns></returns>
        public static List<TreeEntity> ConvertToCheckedTreeList(this List<PowerInfo> objPowerInfoList, long rootId, List<PowerInfo> objCheckedPowerInfoList)
        {
            List<TreeEntity> objTreeEntityList = null;
            if (objPowerInfoList.IsNull())
            {
                return objTreeEntityList;
            }
            var objRoots = objPowerInfoList.Where(c => c.Id == rootId).ToList();
            if (objRoots.IsNull())
            {
                return objTreeEntityList;
            }
            objTreeEntityList = new List<TreeEntity>();
            TreeEntity objRootTreeEntity = null;
            foreach (var objRoot in objRoots)
            {
                objRootTreeEntity = ConvertToCheckedTree(objPowerInfoList, objRoot.Id, objCheckedPowerInfoList);
                if (objRootTreeEntity != null)
                {
                    objTreeEntityList.Add(objRootTreeEntity);
                }
            }
            return objTreeEntityList;
        }
    }
}
